package xin.ganbo.listener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import xin.ganbo.entity.Log;
import xin.ganbo.enums.LogTyep;
import xin.ganbo.service.InitService;
import xin.ganbo.service.LogService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.Date;

/**
 * 系统安装初始化监听器
 * @author ganbo E-mail:bo.gan@56qq.com
 * @since 2017-12-04 10:35
 */

@WebListener
public class Installer implements ServletContextListener{

    private WebApplicationContext beans;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("系统启动开始初始化系统基本数据...");
        beans = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        InitService initService = beans.getBean(InitService.class);
        //初始化系统基本数据
        initService.initSysBaseData();
        System.out.println("<==系统基本数据初始化完毕==>");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("正在关闭服务器....");
        LogService logService = beans.getBean(LogService.class);
        Log log = new Log();
        log.setOperTime(new Date());
        log.setDescription("关闭服务器.");
        log.setLogTyep(LogTyep.BUSINESSLOG);
        logService.save(log);
    }
}
